package com.hnust.service.impl;

import com.hnust.dao.*;
import com.hnust.domain.*;
import com.hnust.domain.Class;
import com.hnust.domain.record.*;
import com.hnust.service.CourseGroupService;
import com.hnust.util.GenerateId;
import com.hnust.util.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建人：张汝往
 * 创建时间：2020/10/7
 * 描述：
 */
@Service("courseGroupService")
public class CourseGroupServiceImpl implements CourseGroupService {

    @Autowired
    private CourseGroupDao courseGroupDao;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private ClassDao classDao;

    @Autowired
    private MajorDao majorDao;

    @Autowired
    private TeacherDao teacherDao;

    /**
     * 获取所有课程
     * @param collegeID 学院ID
     * @return
     */
    @Override
    public List<CourseRecord> getCourses(String collegeID) {
        List<Course> courses=courseDao.findCourseByCollegeId(collegeID);
        if(courses!=null){
            List<CourseRecord> course=new ArrayList<>();
            for(Course c:courses){
                course.add(new CourseRecord(c.getCouId(),c.getName(),c.getCouNumber()));
            }
            return course;
        }else{
            return null;
        }
    }

    /**
     * 获取所有班级
     * @param collegeId 学院ID
     * @return
     */
    @Override
    public List<ClassNameRecord> getClasses(String collegeId) {
        List<Class> classes=classDao.findClassByCollegeId(collegeId);
        if(classes!=null){
            List<ClassNameRecord> className=new ArrayList<>();
            for(Class c:classes){
                String majorName=majorDao.findNameByMajorId(c.getMajId());
                className.add(new ClassNameRecord(
                        c.getGrade()+"级"+majorName+c.getClaNumber()+"班",
                        c.getClaId()
                ));
            }
            return className;
        }else{
            return null;
        }

    }

    /**
     * 获取所有老师
     * @param collegeId 学院ID
     * @return
     */
    @Override
    public List<TeacherNameRecord> getTeachers(String collegeId) {
        List<Teacher> teachers=teacherDao.getTeacherByCollegeId(collegeId);
        if(teachers!=null){
            List<TeacherNameRecord> teaNames=new ArrayList<>();
            for(Teacher t:teachers){
                teaNames.add(
                        new TeacherNameRecord(t.getTeaId(),t.getName(),t.getTeaNumber())
                );
            }
            return teaNames;
        }else{
            return null;
        }
    }

    /**
     * 查询课程组信息
     * @param collegeId 学院ID
     * @return
     */
    @Override
    public List<SimpleCourseGroupInfo> querySimpleCourseGroup(String collegeId) {
       try{
           List<SimpleCourseGroupInfo> simpleCourseGroupInfos=new ArrayList<>();
           List<CourseGroup> courseGroups=courseGroupDao.findAllCourseGroupByCollegeId(collegeId);
           if(courseGroups!=null){
               for(CourseGroup c:courseGroups){
                   List<TeacherNameRecord> teacherNameRecords=new ArrayList<>();
                   List<ClassNameRecord> classNameRecords=new ArrayList<>();
                   List<Teacher> teachers=courseGroupDao.findAllTeacherByCourseGroupId(c.getCouGroId());
                   if(teachers!=null){
                       for(Teacher t:teachers){
                           teacherNameRecords.add(new TeacherNameRecord(t.getTeaId(),t.getName(),t.getTeaNumber()));
                       }
                   }
                   List<Class> classes=courseGroupDao.findAllClassByCourseGroupId(c.getCouGroId());
                   if(classes!=null){
                       for(Class cls:classes){
                           String majorName=majorDao.findNameByMajorId(cls.getMajId());
                           classNameRecords.add(new ClassNameRecord(
                                   cls.getGrade()+"级"+majorName+cls.getClaNumber()+"班",
                                   cls.getClaId()
                           ));
                       }
                   }

                   String courseName=courseDao.findCourseNameById(c.getCouId());

                   simpleCourseGroupInfos.add(new SimpleCourseGroupInfo(
                                   c.getColId(),c.getCouGroId(),courseName,c.getCouId(),c.getOpenTime(),
                           teacherNameRecords,classNameRecords
                           ));
               }
               return simpleCourseGroupInfos;
           }else{
               return null;
           }
       }catch (Exception e){
           return null;
       }
    }

    /**
     * 批量删除课程组
     * @param courseGroupIds 课程组ID们
     * @return
     */
    @Override
    public Boolean batchDeleteCourseGroup(List<String> courseGroupIds) {
        try{
            boolean flag=true;
            for(String s:courseGroupIds){
                if(!courseGroupDao.deleteCourseGroupById(s)){
                    flag=false;
                    break;
                }
            }
            return flag;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 删除课程组
     * @param courseGroupId 课程组ID
     * @return
     */
    @Override
    public Boolean deleteCourseGroup(String courseGroupId) {
        try{
            return courseGroupDao.deleteCourseGroupById(courseGroupId);
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 修改课程组
     * @param editCourseGroupInfo 课程组信息
     * @return
     */
    @Override
    public Boolean modifyCourseGroup(EditCourseGroupInfo editCourseGroupInfo) {
        try{
            if(editCourseGroupInfo.getCourseGroupLeader()!=null&&
                    !editCourseGroupInfo.getCourseGroupLeader().equals("")){
                List<Teacher> teachers= courseGroupDao.findAllTeacherByCourseGroupId(editCourseGroupInfo.getCourseGroupId());
                String oldLeaderId="";
                if(teachers!=null){
                    for(Teacher c:teachers){
                        if(courseGroupDao.findCourseGroupLeaderByCourseGroupIdAndTeacherId(
                                editCourseGroupInfo.getCourseGroupId(), c.getTeaId()
                        )==1){
                            oldLeaderId=c.getTeaId();
                            break;
                        }
                    }
                }
                if(!oldLeaderId.equals(editCourseGroupInfo.getCourseGroupLeader())){
                    courseGroupDao.updateLeaderInTeacherCourseGroup(oldLeaderId,editCourseGroupInfo.getCourseGroupId(),0);
                    courseGroupDao.updateLeaderInTeacherCourseGroup(
                            editCourseGroupInfo.getCourseGroupLeader(),
                            editCourseGroupInfo.getCourseGroupId(),
                            1
                    );
                }
            }
            if(courseGroupDao.modifyCourseGroup(
                    editCourseGroupInfo.getCourseGroupId(),
                    editCourseGroupInfo.getCourseGroupTime(),
                    editCourseGroupInfo.getCourseGroupName(),
                    editCourseGroupInfo.getCourseId()
            )){
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 添加课程组
     * @param addCourseGroupInfo 课程组信息
     * @return
     */
    @Override
    public Boolean addCourseGroup(AddCourseGroupInfo addCourseGroupInfo) {
        try{
            String uuid=GenerateId.create();
            List<String> classIds=addCourseGroupInfo.getClassIds();
            List<String> teacherIds=addCourseGroupInfo.getTeacherIds();
            String courseGroupLeaderId=addCourseGroupInfo.getCourseGroupLeaderId();
            courseGroupDao.addCourseGroup(uuid,addCourseGroupInfo.getCourseGroupName(),
                    addCourseGroupInfo.getCourseGroupTime(),null,addCourseGroupInfo.getCollegeId(),
                    addCourseGroupInfo.getCourseId());
            if(classIds!=null){
                for(String i:classIds){
                    courseGroupDao.insertClassInCourseGroup(GenerateId.create(),i,uuid);
                }
            }
            if(teacherIds!=null&&courseGroupLeaderId!=null){
                if(teacherIds.contains(courseGroupLeaderId)){
                    for(String t:teacherIds){
                        courseGroupDao.insertTeacherInCourseGroup(
                                GenerateId.create(),0,uuid,t
                        );
                    }
                    courseGroupDao.updateLeaderInTeacherCourseGroup(courseGroupLeaderId,uuid,1);
                }else{
                    for(String t:teacherIds){
                        courseGroupDao.insertTeacherInCourseGroup(GenerateId.create(),0,uuid,t);
                    }
                    courseGroupDao.insertTeacherInCourseGroup(GenerateId.create(),1,uuid,courseGroupLeaderId);
                }
            }
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 获取课程组详细信息
     * @param courseGroupId 课程组Id
     * @return
     */
    @Override
    public CourseGroupInfo getCourseGroupInfo(String courseGroupId) {
        try{
            CourseGroup courseGroup=courseGroupDao.findCourseGroupById(courseGroupId);
            String courseName=courseDao.findCourseNameById(courseGroup.getCouId());
            List<Teacher> teachers=courseGroupDao.findAllTeacherByCourseGroupId(courseGroupId);
            List<TeacherNameRecord> teacherNameRecords=new ArrayList<>();
            TeacherNameRecord courseGroupLeader=new TeacherNameRecord();
            if(teachers!=null){
                for(Teacher t:teachers){
                    teacherNameRecords.add(new TeacherNameRecord(t.getTeaId(),t.getName(),t.getTeaNumber()));
                    if(courseGroupDao.findCourseGroupLeaderByCourseGroupIdAndTeacherId(courseGroupId,t.getTeaId())==1){
                        courseGroupLeader.setTeacherId(t.getTeaId());
                        courseGroupLeader.setTeacherName(t.getName());
                        courseGroupLeader.setTeacherNumber(t.getTeaNumber());
                    }
                }
            }
            List<Class> classes=courseGroupDao.findAllClassByCourseGroupId(courseGroupId);
            List<ClassNameRecord> classNameRecords=new ArrayList<>();
            if(classes!=null){
                for(Class c:classes){
                    String majorName=majorDao.findNameByMajorId(c.getMajId());
                    classNameRecords.add(new ClassNameRecord(
                       c.getGrade()+'级'+majorName+c.getClaNumber()+'班',
                       c.getClaId()
                    ));
                }
            }
            return new CourseGroupInfo(
                    new SimpleCourseGroupInfo(
                          courseGroup.getColId(),courseGroup.getCouGroId(),courseName,
                          courseGroup.getCouId(),courseGroup.getOpenTime(),teacherNameRecords,
                          classNameRecords
                    ),courseGroup.getName(),courseGroupLeader
            );
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public Boolean operateCourseGroupTeacher(String courseGroupId, String teacherId, int i) {
        if(i==1){
            String uuid= GenerateId.create();
            return courseGroupDao.insertTeacherInCourseGroup(uuid,0,courseGroupId,teacherId);
        }else{
            return courseGroupDao.deleteTeacherInCourseGroup(courseGroupId,teacherId);
        }
    }

    @Override
    public Boolean operateCourseGroupClass(String courseGroupId, String classId, int i) {
        if(i==1){
            String uuid=GenerateId.create();
            return courseGroupDao.insertClassInCourseGroup(uuid,classId,courseGroupId);
        }else{
            return courseGroupDao.deleteClassInCourseGroup(classId,courseGroupId);
        }
    }
}
